package com.grouplay.dao;

import com.grouplay.entity.Invitation;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("invitationDao")
public interface IInvitationDao {

    @Select("select \n" +
            "i.id id,\n" +
            "i.groupId groupId,\n" +
            "g.groupName groupName,\n" +
            "g.leaderId leaderId,\n" +
            "u.`name` leaderName,\n" +
            "g.beginTime date\n" +
            "from\n" +
            "invitation i,\n" +
            "`group` g,\n" +
            "`user` u\n" +
            "where \n" +
            "i.hasBeenLooked = 0 and \n" +
            "#{id} = i.targetUid and \n" +
            "i.groupId = g.id and \n" +
            "g.isEnd = 0 and \n" +
            "g.leaderId = u.id \n" +
            "order by\n" +
            "i.date\n")
    List<Invitation> inviteMe(Integer id);

    @Update("update invitation set hasBeenLooked = #{hasBeenLooked} where groupId = #{groupId} and targetUid = #{targetUid}")
    void updateInvitation(Invitation invitation);

    @Select("select count(*) from invitation where groupId = #{groupId} and targetUid = #{targetUid} and hasBeenLooked = 0")
    Integer findCountInviteMe(Invitation invitation);
}
